package com.yupi.project.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.project.annotation.AuthCheck;
import com.yupi.project.common.BaseResponse;
import com.yupi.project.common.DeleteRequest;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.common.ResultUtils;
import com.yupi.project.config.ThreadPoolConfig;
import com.yupi.project.constant.UserConstant;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.model.dto.Chart.ChartAddRequest;
import com.yupi.project.model.dto.post.GenChartRequest;
import com.yupi.project.model.entity.Chart;
import com.yupi.project.model.entity.User;
import com.yupi.project.service.ChartService;
import com.yupi.project.service.UserService;
import com.yupi.project.service.impl.AIChart;
import com.yupi.project.service.impl.AIManager;
import com.yupi.project.service.impl.RedissonManager;
import com.yupi.project.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import nonapi.io.github.classgraph.json.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;


@RestController
@RequestMapping("/thread")
@Slf4j
public class ThreadController {

    @Resource
    private ThreadPoolConfig threadPoolConfig;
    @GetMapping("/add")
    public void setThreadPoolConfig(String name) {
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("任务"+name+"当前线程"+Thread.currentThread().getName());
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },threadPoolConfig.getExecutor());
    }
    @GetMapping("/get")
    public String get(){
        ThreadPoolExecutor executor = threadPoolConfig.getExecutor();
        HashMap<String, Object> map = new HashMap<>();
        int size = executor.getQueue().size();
        map.put("当前队列长度",size);
        long taskCount = executor.getTaskCount();
        map.put("任务总数",taskCount);
        long completedTaskCount = executor.getCompletedTaskCount();
        map.put("已完成任务数",completedTaskCount);
        int activeCount = executor.getActiveCount();
        map.put("正在工作的线程数",activeCount);
        return JSONUtil.toJsonStr(map);
    }
}
